package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallGoodsCut;
import com.tencreat.domain.mall.MallIntegralGoods;
import com.tencreat.domain.mall.dto.CutRecordPCDTO;
import com.tencreat.domain.mall.dto.MallAdminGoodsCutDTO;
import com.tencreat.domain.mall.vo.MallGoodsCutStatisticsVO;
import com.tencreat.domain.mall.vo.MallGoodsCutVO;
import com.tencreat.service.mall.IMallGoodsCutService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Map;

//@RestController
@Api(tags = "pc管理端砍价api")
@RequestMapping("/manage/api/cut")
public class AdminMallGoodsCutController extends BaseController {

   // @Autowired
    private IMallGoodsCutService mallGoodsCutService;

    /**
     * 查询砍价商品列表
     */
    @ApiOperation(value = "pc端查询砍价商品列表")
    @PreAuthorize("@ss.hasPermi('system:cut:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallGoodsCutVO>> list(@RequestBody MallAdminGoodsCutDTO dto) {
        return HttpResult.ok(mallGoodsCutService.PCPage(generatePage(dto), dto));
    }

    /**
     * 查询砍价商品详情
     */
    @ApiOperation("pc端查询砍价商品详情")
    @GetMapping("/getDetail")
    public HttpResult<Map<String, Object>> getDetail(@ApiParam("砍价商品id") @RequestParam Long id) {
        return mallGoodsCutService.getDetail(id);
    }


    /**
     * 导出砍价商品列表
     */
    @ApiOperation(value = "导出砍价商品列表")
    @PreAuthorize("@ss.hasPermi('system:cut:export')")
    @Log(title = "营销管理>砍价管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallGoodsCut mallGoodsCut) {
        List<MallGoodsCut> list = mallGoodsCutService.selectMallGoodsCutList(mallGoodsCut);
        ExcelUtil<MallGoodsCut> util = new ExcelUtil<MallGoodsCut>(MallGoodsCut.class);
        return util.exportExcel(list, "cut");
    }


    /**
     * 新增砍价商品
     */
    @ApiOperation(value = "新增砍价商品")
    @PreAuthorize("@ss.hasPermi('system:cut:add')")
    @Log(title = "营销管理>砍价管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody List<MallGoodsCut> mallGoodsCuts) {
        int sums = 0;
        for (MallGoodsCut mallGoodsCut : mallGoodsCuts) {
            sums += mallGoodsCutService.insertMallGoodsCut(mallGoodsCut,getLoginCheck());
        }
        return to(sums);
    }

    /**
     * 修改砍价商品
     */
    @ApiOperation(value = "修改砍价商品")
    @PreAuthorize("@ss.hasPermi('system:cut:edit')")
    @Log(title = "营销管理>砍价管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody MallGoodsCut mallGoodsCut) {
        return to(mallGoodsCutService.updateMallGoodsCut(mallGoodsCut));
    }

    /**
     * 删除砍价商品
     */
    @ApiOperation(value = "删除砍价商品")
    @PreAuthorize("@ss.hasPermi('system:cut:remove')")
    @Log(title = "营销管理>砍价管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody ReqIdsParam ids) {
        return to(mallGoodsCutService.deleteMallGoodsCutByIds(ids));
    }

    @ApiOperation("砍价记录")
    @PostMapping("/cutRecord")
    public HttpResult<IPage<Map<String, Object>>> cutRecord(@Validated @RequestBody CutRecordPCDTO vo) {
        Page<Map<String, Object>> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        return mallGoodsCutService.cutRecord(page, vo);
    }

    @ApiOperation("砍价详情")
    @GetMapping("/cutDetails")
    public HttpResult<IPage<Map<String, Object>>> cutDetails(@RequestParam String cutSn,
                                                             @RequestParam Integer pageNum,
                                                             @RequestParam Integer pageSize) {
        Page<Map<String, Object>> page = new Page<>(pageNum, pageSize);
        return mallGoodsCutService.cutDetails(page, cutSn);
    }

    @ApiOperation("砍价管理金额统计")
    @GetMapping("/cutStatistics")
    public HttpResult<MallGoodsCutStatisticsVO> cutStatistics(){
        return HttpResult.ok(mallGoodsCutService.cutStatistics());
    }

    /*****
     *  将当前分类的排序减1 顺序上升一位
     * @param mallGoodsCut
     * @return
     */
    @PostMapping("/reSortOrderReduce")
    public HttpResult<Boolean> reSortOrderReduce(@RequestBody MallGoodsCut mallGoodsCut){
        return HttpResult.ok(mallGoodsCutService.reSortOrderReduce(mallGoodsCut));
    }

    /*****
     * 将当前分类的排序加1 顺序下降一位
     * @param mallGoodsCut
     * @return
     */
    @PostMapping("/reSortOrderPlus")
    public HttpResult<Boolean> reSortOrderPlus(@RequestBody MallGoodsCut mallGoodsCut){
        return HttpResult.ok(mallGoodsCutService.reSortOrderPlus(mallGoodsCut));
    }
}
